package com.zealcomm.ivcscustomer.service

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.zco.util.ToastUtil
import com.zco.util.listener.CustomOnClickListener
import com.zealcomm.commonui.widget.RecyclerViewDivider
import com.zco.base.BaseManager
import com.zco.ivcs.customer.base.CustomerManager
import com.zealcomm.ivcscustomer.R
import com.zealcomm.ivcscustomer.app.IvcsApplication
import com.zealcomm.commonui.base.BaseFragment
import com.zealcomm.ivcscustomer.databinding.BottomSheetSimpleListBinding
import com.zealcomm.ivcscustomer.databinding.FragmentVideoSettingsBinding
import com.zealcomm.ivcscustomer.databinding.ItemSimpleListBinding
import irtc.base.MediaCodecs.VideoCodec
import irtc.base.VideoCodecParameters
import irtc.base.VideoEncodingParameters

class VideoSettingsFragment : BaseFragment() {

    private var binding: FragmentVideoSettingsBinding? = null
    private val publishInterceptor = CustomerManager.PublishInterceptor()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentVideoSettingsBinding.inflate(inflater, container, false)
        return binding?.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding?.tvResolutionSelector?.setOnClickListener(onClickListener)
        binding?.tvBitRateSelector?.setOnClickListener(onClickListener)
        binding?.tvVideoCodecSelector?.setOnClickListener(onClickListener)
    }

    private val onClickListener = object : CustomOnClickListener<Any>() {
        override fun onCustomClick(view: View?) {
            val data = arrayListOf<String>()
            var selectorType = -1
            when (view?.id) {
                R.id.tv_resolution_selector -> {
                    selectorType = 0
                    data.addAll(arrayListOf("VGA", "720p", "1080p"))
                }
                R.id.tv_bit_rate_selector -> {
                    selectorType = 1
                    data.addAll(arrayListOf("2M", "1.5M", "1M", "768kbps", "512kbps", "384kbps"))
                }
                R.id.tv_video_codec_selector -> {
                    selectorType = 2
                    data.addAll(arrayListOf("vp8", "h264"))
                }
            }
            val bottomSheetDialog = BottomSheetDialog(holdingActivity!!)
            val tempBinding = BottomSheetSimpleListBinding.inflate(layoutInflater)
            tempBinding.root.addItemDecoration(
                RecyclerViewDivider(
                    context!!,
                    LinearLayoutManager.VERTICAL
                )
            )
            tempBinding.root.adapter = object : RecyclerView.Adapter<MyViewHolder>() {
                override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
                    return MyViewHolder(
                        ItemSimpleListBinding.inflate(
                            layoutInflater,
                            parent,
                            false
                        ).root
                    )
                }

                override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
                    holder.textView?.text = data[position]
                    holder.itemView?.setOnClickListener(object : CustomOnClickListener<Any>() {
                        override fun onCustomClick(view: View?) {
                            when (selectorType) {
                                0 -> {
                                    when (data[position]) {
                                        "VGA" -> {
                                            publishInterceptor.width = 640
                                            publishInterceptor.height = 480
                                        }
                                        "720p" -> {
                                            publishInterceptor.width = 1280
                                            publishInterceptor.height = 720
                                        }
                                        "1080p" -> {
                                            publishInterceptor.width = 1920
                                            publishInterceptor.height = 1080
                                        }
                                    }

                                }
                                1 -> {
                                    // TODO 待实现
                                }
                                2 -> {
                                    when (data[position]) {
                                        "vp8" -> {
                                            publishInterceptor.encodes = arrayListOf(
                                                VideoEncodingParameters(
                                                    VideoCodecParameters(VideoCodec.VP8),
                                                    BaseManager.DEFAULT_VIDEO_MAX_BITRATE
                                                )
                                            )
                                        }
                                        "h264" -> {
                                            publishInterceptor.encodes = arrayListOf(
                                                VideoEncodingParameters(
                                                    VideoCodecParameters(VideoCodec.H264),
                                                    BaseManager.DEFAULT_VIDEO_MAX_BITRATE
                                                )
                                            )
                                        }
                                    }
                                }
                            }
                            IvcsApplication.ivcsCustomer?.setPublishInterceptor(publishInterceptor)
                            bottomSheetDialog.dismiss()
                            ToastUtil.showShort("已选择 ${data[position]}")
                        }
                    })
                }

                override fun getItemCount(): Int {
                    return data.size
                }

            }
            bottomSheetDialog.setContentView(tempBinding.root)
            bottomSheetDialog.show()
        }
    }

    class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var textView: TextView? = null

        init {
            textView = itemView as TextView
        }
    }

    override fun initView(view: View?, savedInstanceState: Bundle?) {
    }

    override val layoutId: Int
        get() = R.layout.fragment_video_settings

    override fun onDestroyView() {
        super.onDestroyView()
        binding = null
    }

}